package process;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import beans.ClientInfo;
import beans.HistoriqueFields;
import dao_manager.ClientInfoManager;
import dao_manager.ClientInfoManagerImpl;
import dao_manager.HistoriqueFieldsManager;
import dao_manager.HistoriqueFieldsManagerImpl;

public class ProcessClientModif extends Action{
	
    private static final String ATT_CLIENT_INFO_ID ="Client_info_id";
    private static final String ATT_USER_NAME    = "userName";
    
    Logger l = LoggerFactory.getLogger( ProcessClientModif.class );
    
	public ProcessClientModif()
	{
		super ();
	}

	public ActionForward execute(
	        ActionMapping mapping,
	        ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response)
	        throws Exception {

	    Long client_id=(Long)request.getSession().getAttribute( ATT_CLIENT_INFO_ID ) ;
	    String userName = (String)request.getSession().getAttribute( ATT_USER_NAME ) ;
	    String oldValue, newValue;
	    
	    ClientInfoManager clientInfoManager =new ClientInfoManagerImpl();
	    HistoriqueFieldsManager histotiqueFieldsManager = new HistoriqueFieldsManagerImpl();
	    
	    ClientInfo clientInfo= clientInfoManager.findClientById( client_id );
	    
	    //l.debug(" ID Client  " + client_id + clientInfo.getNomFiliale() );
	    
	    DynaActionForm daf = (DynaActionForm)form;
		
		
		
		if (((String)daf.get( "nomFiliale" )).trim().length() >0)
		{
		    oldValue = ( clientInfo.getNomFiliale()==null ? " -- " : clientInfo.getNomFiliale() );
		    newValue = (String)daf.get( "nomFiliale" );
		    
		    HistoriqueFields hf=new HistoriqueFields(client_id, userName, "nomFiliale", oldValue, newValue ) ;
		    histotiqueFieldsManager.saveHistoriqueFields(hf);
		    
		    clientInfoManager.changeNomFiliale( (String)daf.get( "nomFiliale" ) , client_id );
		}
		
		
		if (((String)daf.get( "nomCorrespondant" )).trim().length() >0)
        {
		    oldValue = ( clientInfo.getNomCorrespondant()==null ? " -- " : clientInfo.getNomCorrespondant() );
            newValue = (String)daf.get( "nomCorrespondant" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "nomCorrespondant", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changeNomCorrespondant( (String)daf.get( "nomCorrespondant" ) , client_id );
        }
		
		if (((String)daf.get( "dateEntreeContact" )).trim().length() >0)
        {
		    oldValue = ( clientInfo.getDateEntreeContact()==null ? " **** " : clientInfo.getDateEntreeContact().toString().substring( 0,10 ) );
            newValue = (String)daf.get( "dateEntreeContact" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "dateEntreeContact", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            clientInfoManager.changeDateEntreeContact( (String)daf.get( "dateEntreeContact" ) , client_id );
        }
		
		if (((String)daf.get( "dateProchainContact" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getDateProchainContact()==null ? " **** " : clientInfo.getDateProchainContact().toString().substring( 0,10 ) );
            newValue = (String)daf.get( "dateProchainContact" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "dateProchainContact", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            clientInfoManager.changeDateProchainContact( (String)daf.get( "dateProchainContact" ) , client_id );
        }
		
		if (((String)daf.get( "dateDernierContact" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getDateDernierContact()==null ? " **** " : clientInfo.getDateDernierContact().toString().substring( 0,10 ) );
            newValue = (String)daf.get( "dateDernierContact" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "dateDernierContact", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            clientInfoManager.changeDateDernierContact( (String)daf.get( "dateDernierContact" ) , client_id );
        }
		
		if (((String)daf.get( "statut" )).trim().length() >0)
        {
		    System.out.println( (String)daf.get( "statut" ) + " ****** 1 ****** " + (String)daf.get( "telephone" ) ) ;
            oldValue = ( clientInfo.getStatut()==null ? " -- " : clientInfo.getStatut() );
            newValue = (String)daf.get( "statut" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "statut", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changeStatut( (String)daf.get( "statut" ) , client_id );
        }
		
		if (((String)daf.get( "telephone" )).trim().length() >0)
        {
		    System.out.println( (String)daf.get( "statut" ) + " ****** 2 ****** " + (String)daf.get( "telephone" ) ) ;
            oldValue = ( clientInfo.getTelephone()==null ? " -- " : clientInfo.getTelephone() );
            newValue = (String)daf.get( "telephone" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "telephone", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changeTelephone( (String)daf.get( "telephone" ) , client_id );
        }
		
		if (((String)daf.get( "poste" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getPoste()==null ? " -- " : clientInfo.getPoste() );
            newValue = (String)daf.get( "poste" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "poste", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changePoste( (String)daf.get( "poste" ) , client_id );
        }
		
		if (((String)daf.get( "mail" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getMail()==null ? " -- " : clientInfo.getMail() );
            newValue = (String)daf.get( "mail" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "mail", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changeMail( (String)daf.get( "mail" ) , client_id );
        }
		
		if (((String)daf.get( "pays" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getPays()==null ? " -- " : clientInfo.getPays() );
            newValue = (String)daf.get( "pays" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "pays", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changePays( (String)daf.get( "pays" ) , client_id );
        }
		
		if (((String)daf.get( "zone" )).trim().length() >0)
        {
            oldValue = ( clientInfo.getZone()==null ? " -- " : clientInfo.getZone() );
            newValue = (String)daf.get( "zone" );
            
            HistoriqueFields hf=new HistoriqueFields(client_id, userName, "zone", oldValue, newValue);
            histotiqueFieldsManager.saveHistoriqueFields(hf);
            
            clientInfoManager.changeZone( (String)daf.get( "zone" ) , client_id );
        }
		
		
		
		
		
		    
		

	        return mapping.findForward("success");
	    }

}
